package edu.dongthang.slides;

import java.io.*;
import java.nio.channels.FileChannel;
import org.apache.poi.hslf.*;
import org.apache.poi.hslf.model.Notes;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFNotes;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
import org.apache.xmlbeans.*;

public class Presentation {
	private XMLSlideShow presentation;
	private XSLFSlide[] slides;
	private String path;
	private static final String TASKLIST = "tasklist";
	public Presentation(String url) throws XmlException {

		this.path = url.toString();
	}

	public String getNote_pptx(String url) throws Exception {
		String note_pptx = "";
		File file = new File(url);
		//checkFileIsOpen(file);
		XMLSlideShow ss = new XMLSlideShow(new FileInputStream(file));

		// InputStream fis = new FileInputStream(file);
		// XMLSlideShow ss = new XMLSlideShow(fis);
		String notes = null;
		for (XSLFSlide slide : ss.getSlides()) {
			XSLFNotes note = slide.getNotes();
			if (note != null) {
				XSLFShape[] shapes = note.getShapes();
				for (int i = 1; i < shapes.length - 1; i++) {
					
					if (shapes[i] instanceof XSLFTextShape) {
						notes = ((XSLFTextShape) shapes[i]).getText();
						//System.out.println("slide " + i + ": " + notes);
						note_pptx += notes.toString()+"!!!";
					}
					
//					if(notes != null){
//							System.out.println("slide " + i + ": " + ((XSLFTextShape) shapes[i]).getText());
//					}
					
				}
			}
			

		}
		return note_pptx;
	}
	
	public static void checkFileIsOpen(File file) throws IOException{
		boolean closed;
		FileChannel channel = null;
        try {
            channel = new RandomAccessFile(file, "rw").getChannel();
            closed = true;
        } catch(Exception ex) {
            closed = false;
        } finally {
            if(channel!=null) {
                try {
                    channel.close();
                } catch (IOException ex) {
                    // exception handling
                }
            }
        }
		  if(closed == false)
			  System.out.print("file is open");
		  else
			  System.out.print("file is close");
	}
	
//	public static boolean isProcessRunging(String serviceName) throws Exception {
//		 
//		 Process p = Runtime.getRuntime().exec("tasklist.exe");
//		 BufferedReader reader = new BufferedReader(new InputStreamReader(
//		   p.getInputStream()));
//		 String line;
//		 while ((line = reader.readLine()) != null) {
//		 
//		  //System.out.println(line);
//		  if (line.contains(serviceName)) {
//		   return true;
//		  }
//		 }
//		 return false;
//		 
//		}
	
	
	public String getNote_ppt(String url) throws Exception {
		String note_ppt = "";
		File file = new File(url);
		//checkFileIsOpen(file);
		InputStream fis = new FileInputStream(file);
		POIFSFileSystem fs = new POIFSFileSystem(fis);
		HSLFSlideShow show = new HSLFSlideShow(fs);

		SlideShow ss = new SlideShow(show);
		Slide[] slides = ss.getSlides();
		for (int i = 0; i < slides.length; i++) {
			//System.out.println("slide " + (i + 1));

			TextRun[] runs = slides[i].getTextRuns();

			Notes notes = slides[i].getNotesSheet();
			if (notes != null) {
				runs = notes.getTextRuns();
				for (int j = 0; j < runs.length; j++) {
					//System.out.println("notes text run " + (j + 1) + ": "
							//+ runs[j].getText());
					note_ppt += runs[j].getText()+"!!!";
				}
			}
		}
		return note_ppt;
	}

//	public static void main(String[] args) throws Exception {
//		Presentation note = new Presentation("C:/Users/Dong Thang/Desktop/congnghephanmem_1.pptx");
//		
//		//MyNote note = new MyNote("C:/Users/Dong Thang/Desktop/Công nghệ phần mềm_ nhóm 9.pptx");
//		// MyNote("C:/Users/Dong Thang/Desktop/congnghephanmem_1.ppt);
//		
//		String urls = note.path;
//		String extend = urls.substring(urls.lastIndexOf(".")+1);
////		if (extend.equalsIgnoreCase("pptx")) {
////			try {
////				getNote_pptx(urls);
////			} catch (Exception e) {
////				// TODO: handle exception
////			}
////		}else{
////			try {
////				getNote_ppt(urls);
////			} catch (Exception e) {
////				// TODO: handle exception
////			}
////		}
//		
//		String processName = "POWERPNT.EXE";
//		if (isProcessRunging(processName)) {
//			System.out.println("file ppt dang mo");
//			  
//			 }
//		else
//			System.out.println("file ppt dang dong");
//
//	}
}
